<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <router-link :to="{ name: 'AdsAdd' }">
        <a-button type="primary" icon="plus">新建</a-button>
      </router-link>
    </div>

    <a-table
      ref="table"
      :row-key="
        (record) => {
          return record.broadcastId
        }
      "
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="status" slot-scope="text, record">
        <a-switch
          checked-children="开启"
          un-checked-children="关闭"
          @change="
            (checked) => {
              changeStatus(record.broadcastId, checked)
            }
          "
          :checked="record.status == 1 ? true : false"
        />
      </span>
      <span slot="photo" slot-scope="photo">
        <!-- {{photo}} -->
        <img :src="photo" width="100px" />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <router-link :to="{ name: 'AdsEdit', params: { id: record.broadcastId } }">修改 </router-link>
          <a-divider type="vertical" />
          <a @click="deleteOne(record.broadcastId)">删除</a>
        </template>
      </span>
    </a-table>
  </a-card>
</template>
<script>
const columns = [
  {
    title: '序号',
    dataIndex: 'broadcastId'
  },
  {
    title: '轮播图名称',
    dataIndex: 'broadcastName'
  },
  {
    title: '图片',
    dataIndex: 'broadcastImage',
    scopedSlots: { customRender: 'photo' }
  },
  {
    title: '跳转地址',
    dataIndex: 'broadcastUrl'
  },
  // {
  //   title: '排序',
  //   dataIndex: 'sort'
  // },
  {
    title: '状态',
    dataIndex: 'statusText',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '创建时间',
    dataIndex: 'createdAt'
  },
  {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'AdsList',
  data() {
    return {
      id: this.$route.params.id || 0,
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (
            <div>
              共 <span>{total}</span> 条
            </div>
          )
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      }
    }
  },
  created() {
    // 获取列表
    this.fetch()
  },
  methods: {
    handleProvinceChange() {},
    // 改变每页数量时更新显示
    onChange(pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn() {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch() {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
      }
      this.$common
        .handleGet('/broadcasts/lists', params)
        .then((data) => {
          this.loading = false
          if (data.code === 200) {
            const result = data.data
            const pagination = { ...this.pagination }
            pagination.total = result.totalInfo.total
            this.data = result.lists
            this.pagination = pagination
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 启用、禁用
    changeStatus(id, status) {
      if (id && status !== '') {
        var url = ''
        if (status) url = '/broadcasts/enable/'
        else url = '/broadcasts/disable/'
        this.$common
          .changeField(url + id, {})
          .then((data) => {
            if (data.code === 200) {
              this.$message.success(data.message)
              console.log(data)
              this.fetch()
            } else {
              this.$message.error(data.message)
              console.log(data.message)
            }
          })
          .catch(() => {
            this.$notification.error({
              message: '错误',
              description: '请求信息失败，请重试'
            })
          })
      } else {
        this.$message.error('参数错误')
      }
    },
    // handleAdd () {
    //   this.mdl = { roleName: '', status: 1, gender: 1 }
    //   this.visible = true
    // },
    // handleEdit (record) {
    //   this.visible = true
    // },
    // handleOk () {
    //   this.visible = false
    //   // 刷新表格
    //   this.fetch()
    // },
    // handleCancel () {
    //   this.visible = false
    // },
    // 删除某一个
    deleteOne(id) {
      this.handleDelete(id)
    },
    handleDelete(id) {
      const _this = this
      const params = {}
      this.$common
        .delete('/broadcasts/delete/' + id, params, [id])
        .then((res) => {
          if (res) {
            _this.fetch()
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    }
  }
}
</script>
